﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EngineOfEvermore.Common;
using System.Diagnostics;

namespace EngineOfEvermore.Scene.Physics
{
    public class CollisionComponent : SceneObjectComponent
    {
        public struct ShapeCollection : IEnumerable<CollisionShape>
        {
            CollisionComponent _owner;

            internal ShapeCollection( CollisionComponent owner )
            {
                _owner = owner;
            }

            public void Add( CollisionShape shape )
            {
                Debug.Assert( !_owner._shapes.Contains( shape ) );

                _owner._shapes.Add( shape );

                if ( _owner.CollisionShapeAdded != null )
                {
                    _owner.CollisionShapeAdded( _owner, shape );
                }
            }

            public void Clear()
            {
                while ( Count > 0 )
                {
                    Remove( _owner._shapes[_owner._shapes.Count - 1] );
                }
            }

            public bool Contains( CollisionShape item )
            {
                return _owner._shapes.Contains( item );
            }

            public int Count
            {
                get { return _owner._shapes.Count; }
            }

            public bool Remove( CollisionShape shape )
            {
                bool shapeRemoved = _owner._shapes.Remove( shape );

                if ( shapeRemoved && _owner.CollisionShapeRemoved != null )
                {
                    _owner.CollisionShapeRemoved( _owner, shape );
                }

                return shapeRemoved;
            }

            public IEnumerator<CollisionShape> GetEnumerator()
            {
                return _owner._shapes.GetEnumerator();
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return _owner._shapes.GetEnumerator();
            }
        }

        public struct Contact
        {
            public readonly SceneObject SceneObject;
            public readonly Vector2d Position;

            public Contact( SceneObject sceneObject, Vector2d position )
            {
                this.SceneObject = sceneObject;
                this.Position = position;
            }
        }

        #region Variables
        List<CollisionShape> _shapes = new List<CollisionShape>();
        List<Contact> _contacts = new List<Contact>();
        float _weight;
        #endregion

        #region Events
        public event Action<CollisionComponent, float> WeightChanged;
        public event Action<CollisionComponent, CollisionShape> CollisionShapeAdded;
        public event Action<CollisionComponent, CollisionShape> CollisionShapeRemoved;
        #endregion

        #region Properties
        public ShapeCollection Shapes
        {
            get { return new ShapeCollection( this ); }
        }

        public IEnumerable<Contact> Contacts
        {
            get { return _contacts; }
        }

        public float Weight
        {
            get { return _weight; }
            set 
            {
                Debug.Assert( value >= 0.0f );

                if ( _weight != value )
                {
                    _weight = value;

                    if ( WeightChanged != null )
                    {
                        WeightChanged( this, value );
                    }
                }
            }
        }
        #endregion

        #region Methods
        
        internal void AddContact( SceneObject sceneObject, Vector2d position )
        {
            _contacts.Add( new Contact( sceneObject, position ) );
        }

        internal void ClearContacts()
        {
            _contacts.Clear();
        }
        #endregion

        #region SceneObjectComponent Overrides
        internal protected override void OnAddToScene( SceneGraph scene )
        {
            base.OnAddToScene( scene );

            CollisionSubSystem css = scene.GetSubSystem<CollisionSubSystem>();

            css.RegisterCollisionComponent( this );
        }

        internal protected override void OnRemoveFromScene( SceneGraph scene )
        {
            CollisionSubSystem css = scene.GetSubSystem<CollisionSubSystem>();

            css.UnregisterCollisionComponent( this );

            base.OnRemoveFromScene( scene );
        }
        #endregion
    }
}
